<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="学生团员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <XSearchLeagueMember
                :disabled="userDisabled"
                :param="{ userId: userId }"
                v-decorator="[
                  'memId',
                  {
                    rules: [{ required: true, message: '请选择团员!' }]
                  }
                ]"
              ></XSearchLeagueMember>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="团费缴纳状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="[
                  'payStatus',
                  {
                    rules: [{ required: true, message: '团费缴纳状态!' }]
                  }
                ]"
                :trigger-change="true"
                dictCode="league_mem_fee_pay_status"
                placeholder="请选择团费缴纳状态"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="缴纳金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                v-decorator="[
                  'payAmount',
                  {
                    rules: [{ required: true, message: '请输入缴纳金额!' }]
                  }
                ]"
                placeholder="请输入缴纳金额"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="缴纳时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择缴纳时间"
                v-decorator="[
                  'payTime',
                  {
                    rules: [{ required: true, message: '请选择缴纳时间!' }]
                  }
                ]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="缴纳起止时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-range-picker
                name="payTimeRange"
                style="width: 100%"
                v-decorator="['payTimeRange', { rules: [{ required: true, message: '请选择缴纳起止时间' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import XSearchLeagueMember from '@/components/extends/XSearchLeagueMember'
export default {
  name: 'LeagueMembershipDuesForm',
  components: {
    JFormContainer,
    JDate,
    JDictSelectTag,
    XSearchLeagueMember
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      userId: '',
      userDisabled: false,
      editableFiexd: {},
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/leagueMemFee/add',
        edit: '/leagueMemFee/edit',
        queryById: '/leagueMemFee/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    changeMember(data) {
      let obj = {
        memId: data.id
      }
      this.form.setFieldsValue(obj)
    },
    add(userId, userDisabled) {
      this.userId = userId
      this.userDisabled = userDisabled
      this.open({})
    },
    edit(record) {
      this.userDisabled = true
      this.userId = ''
      this.open(record)
    },
    open(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.model.payTime = record.payTime || null
      this.model.payTimeRange = [record.startTime || null, record.endTime || null]
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'memId', 'payAmount', 'payTime', 'payTimeRange', 'payStatus'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then(res => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      // 触发表单验证
      const that = this
      return new Promise((resolve, reject) => {
        that.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!that.model.id) {
              httpurl += that.url.add
              method = 'post'
            } else {
              httpurl += that.url.edit
              method = 'put'
            }
            let formData = Object.assign(that.model, values)
            formData.startTime = formData.payTimeRange[0].valueOf()
            formData.endTime = formData.payTimeRange[1].valueOf()
            httpAction(httpurl, formData, method)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }
        })
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'memId', 'payAmount', 'payTime', 'payTimeRange', 'payStatus'))
    }
  }
}
</script>
